﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using BoolanElement;
using BoolanElement.Storage;
using BoolanSlide.Document.Format.UIElement.Vote;
using Windows.Data.Xml.Dom;
using Windows.Storage;

namespace BoolanSlide.FileStruct
{

    public static class BooDocumentHelper
    {
        public static Type[] ElementNames = { 
            typeof(BooTableElement),
            typeof(BooEllipseElement),
            typeof(BooPictureElement),
            typeof(BooRoundRectElement),
            typeof(BooLineElement),
            typeof(BooEllipseDialogElement),
            typeof(BooRectangleDialogElement),
            typeof(BooArrowElement),
            typeof(BooRectangleElement),
            typeof(BooTriangleElement),
            typeof(BooRightTriangleElement),
            typeof(BooPentagonElement),
            typeof(BooRightArrowElement),
            typeof(BooTwoArrowsElement),
            typeof(BooDiamondElement),
            typeof(BooTextBoxElement),
            typeof(BooFivePointedElement),
            typeof(BooCurveElement),
            typeof(VoteElement)
        };

        public static string ImageFolder = "SlideIdea/tmp/opened";
        public static string TemplateImageFolder = "SlideIdea/tmp/template";
        public static string CoverImageFolder = "SlideIdea/tmp/cover";
        public static string ImageTmpFolder = "SlideIdea/tmp/image";
        public static string BakFolder = "SlideIdea/tmp/bak";
        public static string LibraryFolder = "SlideIdea/SlideIdeaPPT";
        public static string ConfigFolder = "SlideIdea/config";
        public static async Task<BooDocument> NewFile(BPTFileInformation TemplateFileInformation)
        {
            //在内存中创建文档
            BooDocumentInMemory doc = await BPTFile.CreateNewFile(TemplateFileInformation);
            //解压图片
            await doc.CreateImageFile();
            //生成文档
            BooDocument booDoc = await LoadFromMemory(doc);
            return booDoc;
        }

        public static async Task<BooDocument> FromLibrary(String StorageFileName)
        {
            string tmpname = await GetExtractFolder();

            StorageFolder library = await BooUtility.GetFolderFromDocumentFolderAsync(BooDocumentHelper.LibraryFolder);

            StorageFile file = await library.GetFileAsync(StorageFileName);

            BooDocument doc = await ExtractDocument(tmpname, file);
            return doc;
        }

        public static async Task<BooDocument> FromFileSystem(StorageFile storageFile)
        {
            string tmpname = await GetExtractFolder();
            BooDocument doc = await ExtractDocument(tmpname, storageFile);
            return doc;
        }

        private static async Task<BooDocument> ExtractDocument(string Folder, StorageFile storageFile)
        {
            //await BPTFile.Extract(Folder, storageFile);
            //BooDocument doc = await LoadFromPathAsync(Folder);
            BooDocumentInMemory document = await BPTFile.OpenFile(storageFile);
            await document.CreateImageFile();

            BooDocument doc = await BooDocumentHelper.LoadFromMemory(document);
            

            doc.FileInformation.FileName = storageFile.Name;
            return doc;
        }

        private static async Task<string> GetExtractFolder()
        {
            String tmpname = "boolan.tmp";
            StorageFolder root = await KnownFolders.DocumentsLibrary.CreateFolderAsync(tmpname, CreationCollisionOption.OpenIfExists);
            await BooUtility.DeleteFolder(root);
            return tmpname;
        }

        public static async Task<BooDocument> LoadFromMemory(BooDocumentInMemory document)
        {
            BooDocument doc = new BooDocument();
            XmlDocument summary = await document.GetSummary();
            int slideNum = Convert.ToInt32(summary.GetElementsByTagName("Count").ElementAt(0).InnerText);
            doc.FileInformation.FileName = document.FileName;
            doc.FileInformation.StorageName = summary.GetElementsByTagName("StorageName").ElementAt(0).InnerText;

            doc.FileInformation.Resolution = (BPTFileInformation.FileRatio)Convert.ToInt32(summary.GetElementsByTagName("Resolution").First().InnerText);

            if (summary.GetElementsByTagName("CreateTime").Length > 0)
                doc.FileInformation.CreateTime = Convert.ToDateTime(summary.GetElementsByTagName("CreateTime").ElementAt(0).InnerText);
            if (summary.GetElementsByTagName("ThemeGUID").Length > 0)
                doc.FileInformation.ThemeGuid = summary.GetElementsByTagName("ThemeGUID").First().InnerText;
            doc.Slides=await document.GetSlide();
            foreach (var item in doc.Slides)
            {
                item.AttachToDocument(doc);
            }
            return doc;
        }

        public async static Task<bool> PrepareSlideFile(StorageFile slideFile)
        {
            XmlDocument doc = new XmlDocument();
            doc.AppendChild(doc.CreateElement("BoolanPPT"));
            await doc.SaveToFileAsync(slideFile);
            return true;
        }

    }
}
